//定义的一个节点类
function TreeNode(name, href){
	var s = this;//定义私有变量s来保存this指针，用于函数的回调
	this.level = 0;	
	this.isHover = false;
	this.parent = null;
	this.isClose = false;
	this.children = [];
	this.isleaf = true;
	
	//引入所需要的图片
	this.arrow_collapsed = "resource/image/arrow_collapsed.gif";
	this.arrow_collapsed_hover = "resource/image/arrow_collapsed_hover.gif";
	this.arrow_expanded = "resource/image/arrow_expanded.gif";
	this.arrow_expanded_hover = "resource/image/arrow_expanded_hover.gif";
	this.folder_closed = "resource/image/folder_closed.gif";
	this.folder_open = "resource/image/folder_open.gif";
	this.empty = "resource/image/empty.gif";
	this.leaf = "resource/image/tree_node.gif";
	
	this.container = document.createElement("div");
	this.content = document.createElement("div");
	this.indentSpace = document.createElement("span");
	this.arrow = document.createElement("img");
	this.folder = document.createElement("img");
	this.label = document.createElement("a");
	this.childSpace = document.createElement("div");

	this.content.style.display = "inline";
	this.arrow.align = "absmiddle";
	this.folder.align = "absmiddle";
	this.label.style.lineHeight = "20px";
	this.label.style.fontSize = "12px";
	this.label.style.color = "black";
	
	this.container.appendChild(this.indentSpace);
	this.container.appendChild(this.content);
	
	this.content.appendChild(this.arrow);	
	this.content.appendChild(this.folder);
	this.content.appendChild(this.label);
	this.container.appendChild(this.childSpace);
	this.label.style.textDecoration = "none";
	this.label.innerHTML = name;
	
	this.show = function() {
		for(var i=0; i<this.level; i++) {
			var space = document.createElement("img");
			space.src="resource/image/empty.gif";
			this.indentSpace.appendChild(space);
		}
		if(this.parent == null) {
			this.isClose = false;
		}else {
			this.isClose = true;	
		}
		this.childSpace.style.display = this.isClose?"none":"block";
		if(this.isleaf) {
			this.folder.src = this.leaf;
			this.arrow.src = this.arrow_collapsed;
		}else {
			this.folder.src = this.isClose?this.folder_closed:this.folder_open;
			this.arrow.src = this.isClose?this.arrow_collapsed:this.arrow_expanded;
		}
	};

	//添加子结点
	this.add = function(child) {
		this.children.push(child);
		child.parent = this;
		this.isleaf = false;
		child.level = this.level + 1;
	};
	
	//鼠标移到目标上面是触发的事件
	this.content.onmouseover = function() {
		s.isHover = true;
		if(s.isClose == true) {
			s.arrow.src = s.arrow_collapsed_hover;
		}else {
			s.arrow.src = s.arrow_expanded_hover;
		}
		s.label.style.cursor = "default";
	};
	//鼠标离开目标上面是触发的事件
	this.content.onmouseout = function() {
		s.isHover = false;
		if(s.isClose == true) {
			s.arrow.src = s.arrow_collapsed;
		}else {
			s.arrow.src = s.arrow_expanded;
		}
	};
	
	//鼠标点击目标时触发的事件
	this.content.onclick = function() {
		if(s.isleaf==false) {
			if(s.isClose==true) {
					s.arrow.src = s.arrow_expanded_hover;
					s.folder.src = s.folder_open;
					s.childSpace.style.display = "block";
					s.isClose = false;
			}else{
				s.arrow.src = s.arrow_collapsed_hover;
				s.folder.src = s.folder_closed;
				s.childSpace.style.display = "none";
				s.isClose = true;
			}
		}else if(href) {
			window.parent.frames["right"].document.getElementById("rightFrame").src = href;
		}
	};
}